home *** CD-ROM | disk | FTP | other *** search
- /************************************
- *
- * file: SimpleOutSound.c
- *
- * Program: SimpleOutMovies
- * This program creates a movie by spooling in PICTs from a folder
- * and adding adding them one at a time to the movie. When all PICTs have
- * been processed a sound track is added if one is present.
- *
- * In this file we have all the routines that allow to add
- * a sound track to the movie.
- *
- * The user is asked to select the file containing the sound.
- * The data can be in a 'snd ' resource or in the data fork of the file
- * depending in the method used to add the samples.
- *
- * By Guillermo A. Ortiz
- * April 1991
- *
- * Apple Macintosh Developer Technical Support
- *
- * Copyright © 1989-1991 Apple Computer, Inc.
- * All rights reserved.
- *
- ************************************/
- #define USEDUMP
-
-
-
- #if !defined(USEDUMP)
-
- #include <Types.h>
- #include <QuickDraw.h>
- #include <ToolUtils.h>
- #include <Events.h>
- #include <Controls.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Menus.h>
- #include <Desk.h>
- #include <SegLoad.h>
- #include <Files.h>
- #include <OSEvents.h>
- #include <Traps.h>
- #include <Fonts.h>
- #include <OSUtils.h>
- #include <Resources.h>
- #include <Memory.h>
- #include <Packages.h>
-
- #include <Lists.h>
- #include <Files.h>
- #include <errors.h>
- #include <aliases.h>
- #include <Movies.h>
- #include <MoviesFormat.h>
-
- #pragma dump "SimpleOutSound.dump"
-
- #else
- #pragma load "SimpleOutSound.dump"
- #endif
-
- /* The following def changes the behavior of the code in the method used
- to add the samples; when defed the data is not moved into the movie
- file but left in the original file. Note that the resulting movie could
- be made a 'normal' movie by calling FlattenMovie on it.
- */
- // #define __UseExternalFile__
-
- /* Again for the sake of simplicity (a good excuse)
- we are assuming here that the 'snd ' resource is format 1 and contains
- only one command with 11Kz sampled sound data. Other resources would have
- to be parsed in a more detailed way in order to get more info out
- of them.
- */
-
- #define kTimeScale 11000 /* samples per second */
- #define kSoundTrackVolume 0x0100 /* small fixed from -1 to 1 */
-
- /* this defs help get the different fields in the 'snd ' resource */
- #define kDataOffset 0x2AL /* start of real data in 'snd ' */
- #define kRateOffset 0x1C /* offset to rate for samples in 'snd ' */
- #define kCountOffset 0x18 /* offset to number of samples in 'snd ' */
-
- OSErr AddSoundTrack( Movie, Handle);
- extern Handle MyGetSF2(void);
- extern OSErr MyGetSF(FSSpec *);
-
- void CreateDescriptorFromSnd(Handle, SoundDescriptionHandle);
-
- /* Stuff for adding the sample */
- TimeValue sampTime; /* Time sample is added to media */
- Track gTrack;
- Media gMedia;
-
- OSErr AddSoundTrack( moov, mdrh)
- Movie moov;
- Handle mdrh; /* an alias handle for sure */
- {
-
- short theErr = noErr;
- Handle soundDataH;
- SoundDescriptionHandle sndDescriptH;
- FSSpec mySpec;
- AliasHandle SoundFileAlias;
-
- #ifdef __UseExternalFile__
- /* When the data is external a ned DatRef record is needed in order indicate the
- container for the data. In the case when we add to the same movie file we keep
- using the reference created when the movie was opened.
- */
- if (theErr = MyGetSF(&mySpec) )
- return theErr;
- theErr = NewAlias(nil, &mySpec, &SoundFileAlias);
- DisposHandle(mdrh);
- /*** Media data references disappeared, now we use aliases only ***/
- mdrh = (Handle)SoundFileAlias;
-
- if (!theErr ) {
- #else
- if ( (soundDataH = MyGetSF2()) ) { /* the user selected a file and we were able to read the data in */
- #endif
-
- /* Now we allocate a handle for the sound descriptor */
- sndDescriptH = (SoundDescriptionHandle) NewHandle(sizeof(SoundDescription));
- if (! sndDescriptH ) /* couldn't get the handle */
- theErr = MemError();
- else {
- CreateDescriptorFromSnd(soundDataH,sndDescriptH);
-
- /* From previous file we know that NewMovieTrack changed
- gTrack = NewMovieTrack(moov,(TimeValue) 0,kTimeScale,0,0); */
- gTrack = NewMovieTrack(moov,0,0, kSoundTrackVolume ); /* sound uses no space */
- if (theErr = GetMoviesError()) DebugStr("\pNewMovieTrack Failed");
-
- /****** Changed to take aliases directly *******/
- gMedia = NewTrackMedia(gTrack, SOUND_TYPE, kTimeScale, mdrh, 'alis');
- if (theErr = GetMoviesError()) DebugStr("\pNewTrackMedia Failed");
-
- #ifndef __UseExternalFile__
- theErr = BeginMediaEdits( gMedia ); /* We do this since we are adding samples to the media */
- if (theErr) DebugStr("\pBeginMediaEdits Failed");
- #endif
- #ifdef __UseExternalFile__
- theErr = AddMediaSampleReference(gMedia,0,0x1F780L,
- (TimeValue)1,(SampleDescriptionHandle) sndDescriptH,0x1F780L, /* I know, I know. You can do it better! */
- 0, &sampTime);
- if (theErr) DebugStr("\pAddMediaSample Failed");
- #else
- theErr = AddMediaSample(gMedia,soundDataH,kDataOffset,GetHandleSize(soundDataH),
- (TimeValue)1,(SampleDescriptionHandle) sndDescriptH,*((long *)((*soundDataH) + kCountOffset)), /* !!!!!!! */
- 0, &sampTime);
- if (theErr) DebugStr("\pAddMediaSample Failed");
-
- theErr = EndMediaEdits( gMedia ); /* We're done adding samples */
- if (theErr) DebugStr("\pEndMediaEdits Failed");
-
- DisposHandle(soundDataH);
- #endif
- /******* name and parameter changed *****/
- theErr = InsertMediaIntoTrack(gTrack,0L,0L,GetMediaDuration(gMedia), 0x00010000);
- if (theErr) DebugStr("\pInsertTrackMedia Failed");
- DisposHandle((Handle) sndDescriptH);
- }
- }
- return theErr;
- }
-
- /* This proc fills in the sound description structure necessary to add
- data to a sound media.
-
- */
-
-
- void CreateDescriptorFromSnd(sndRes, sndDescptrH)
- Handle sndRes;
- SoundDescriptionHandle sndDescptrH;
- {
- SoundDescriptionPtr sndDescriptP;
- Ptr sndResP;
-
- #ifndef __UseExternalFile__
- sndResP = *sndRes;
- #endif
- sndDescriptP = *sndDescptrH; /* Deref to save some *'s */
-
- /**** THESE FIELDS HAVE DIFFERENT NAMES NOW ***/
- sndDescriptP->descSize = sizeof(SoundDescription); /* total size of SoundDescription including extra data */
- sndDescriptP->dataFormat = 'raw '; /* my sound comes from a 'snd ' */
- sndDescriptP->resvd1 = 0; /* reserved */
- sndDescriptP->resvd2 = 0; /* reserved */
- sndDescriptP->version = 0; /* which version is this data */
- sndDescriptP->revlevel = 0; /* what version of that codec did this */
- sndDescriptP->vendor = 0; /* whose codec compressed this data */
- sndDescriptP->compressionID = 0; /* sound compression used, 0 if none */
- sndDescriptP->packetSize = 0; /* packet size for compression, 0 if no compression */
- sndDescriptP->numChannels = 1; /* number of channels of sound */
- sndDescriptP->sampleSize = 8; /* number of bits per sample */
- #ifdef __UseExternalFile__
- sndDescriptP->sampleRate = 0x2b7745d1L; /* Yeepee! */
-
- #else
- sndDescriptP->sampleRate =
- *((long *)(sndResP + kRateOffset)); /* sample rate sound is captured at */
-
- #endif
- }